package com.controller;

import com.util.PageBean;
import com.bean.Test;
import com.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


@Controller
@RequestMapping("/Test")
public class TestController  extends BaseController{

    @Autowired
    private TestService testService;

    @RequestMapping("/A")
    public ModelAndView TestA(){
        ModelAndView mv = new ModelAndView("index");
        System.out.printf("gg");
        return  mv;
    }

    @ResponseBody
    @RequestMapping("/B")
    public Map<String,Object> getJson(){
        Map<String,Object> map = new HashMap<>();
        map.put("123","abcv");
        map.put("456","hhh");
        return map;
    }

    //返回主页
    @RequestMapping("goHome")
    public ModelAndView goHome(){
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }

    //添加
    @RequestMapping("addTest")
    public ModelAndView addTest(Test test,RedirectAttributes attr){
        ModelAndView mv = new ModelAndView("redirect:selTestAll");
        testService.insert(test);
        attr.addAttribute("msg","add");
        return mv;
    }
    //修改
    @RequestMapping("updateTest")
    public ModelAndView updateTest(Test test,RedirectAttributes attr){
        ModelAndView  mv = new ModelAndView("redirect:selTestAll");
        testService.updateTest(test);
        attr.addAttribute("msg","update");
        return  mv;
    }
    //删除
    @RequestMapping("delTest")
    public ModelAndView deleteTest(long id,RedirectAttributes attr){
        ModelAndView mv = new ModelAndView("redirect:selTestAll");
        testService.deleteTest(id);
        attr.addAttribute("msg","del");
        return  mv;
    }
    //按id查询 ajax方式
    @ResponseBody
    @RequestMapping("selById")
    public Map<String,Object> selById(long id){
        Map<String,Object> map = new HashMap<>();
        Test test = testService.selTestByid(id);
        map.put("test",test);
        return  map;
    }

    //分页条件查询
    @RequestMapping("selTestAll")
    public ModelAndView selTestAll(@RequestParam(defaultValue="1")int page, @RequestParam(defaultValue="5")int rows,Test test){
        ModelAndView mv = new ModelAndView("test");
        PageBean<Test> pageBean = testService.selTestAll(page,rows,test);
        List<Test> tlist = pageBean.getList();
        //tlist.stream().forEach(System.out::println);
        mv.addObject("tlist",tlist);
        mv.addObject("page",pageBean.getPageNum());
        mv.addObject("pages",pageBean.getPages());
        mv.addObject("rows",pageBean.getPageSize());
        mv.addObject("total",pageBean.getTotal());
        mv.addObject("test",test);
        return  mv;
    }
    @ResponseBody
    @RequestMapping("selAll")
    public Map selAll(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int rows,String testA,String testB){
        Map map=new HashMap();
        Test test=new Test();
        test.setTestA(testA);
        test.setTestB(testB);
        PageBean<Test> pageBean=testService.selTestAll(page,rows,test);
        List<Test> tlist=pageBean.getList();
        map.put("tlist",tlist);
        map.put("page",pageBean.getPageNum());
        map.put("pages",pageBean.getPages());
        map.put("rows",pageBean.getPageSize());
        map.put("total",pageBean.getTotal());
        map.put("test",test);
        return map;
    }
    @RequestMapping("sel")
    public ModelAndView sel(){
        ModelAndView mv=new ModelAndView("selAll");
        return mv;
    }
    @ResponseBody
    @RequestMapping("add10")
    public Map add10(){
        Map map=new HashMap();
        for (int j=0;j<10;j++) {
            Random random = new Random();
            int max=9999;
            int min=1000;
            int s = random.nextInt(max)%(max-min+1) + min;
            Test test = new Test();
            String sss=String.valueOf(s);
            test.setTestA(sss);
            test.setTestB(sss);
            testService.insert(test);
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("delById")
    public Map del(Long id){
        Map map=new HashMap();
        int i=testService.deleteByPrimaryKey(id);
        return map;
    }
    @ResponseBody
    @RequestMapping("selById1")
    public Map selById1(Long id){
        Map map=new HashMap();
        Test test=testService.selTestByid(id);
        map.put("test",test);
        return map;
    }
    @ResponseBody
    @RequestMapping("upTest")
    public Map upTest(Long id,String testA,String testB){
        Map map=new HashMap();
        Test test=new Test();
        test.setId(id);
        test.setTestA(testA);
        test.setTestB(testB);
        int i=testService.updateTest(test);
        return map;
    }
}

